#include "tool.h"
#include <QFile>
#include <QDateTime>

QString Tool::toUpperAndLower(QString src)
{
    for(int i = 0; i < src.count(); i++) {
        QChar curC = src.at(i);
        if(curC.isUpper()) {
            curC = curC.toLower();
        } else if(curC.isLower()){
            curC = curC.toUpper();
        }
        src[i] = curC;
    }
    return src;
}

QString Tool::fileRead(const QString &fp)
{
    QFile file(fp);
    QString res = "";
    if (file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        while (!file.atEnd())
        {
            QByteArray line = file.readLine();
            QString str =  QString::fromLocal8Bit(line);
            res += str;
        }
        file.close();
      }
    return res;
}

QString Tool::RandomQString() {
    qsrand(QDateTime::currentMSecsSinceEpoch());//为随机值设定一个seed

    const char chrs[] = "0123456789abcdefghijklmnopqrstuvwxyz0123456789";//这里平衡了字母和数字出现的频率
    int chrs_size = sizeof(chrs);
    char* ch = new char[5];
    int randomx = 0;

    memset(ch, 0, 5);

    for (int i = 0; i < 4; i++)                 //生成随机验证码
    {
        randomx= rand() % (chrs_size - 1);
        ch[i] = chrs[randomx];
    }

    QString s = ch;
    return s;
}
